﻿using OpenTK.Graphics.OpenGL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ImageTour
{
    public partial class MainForm : Form
    {
        private Scene scene;

        public MainForm()
        {
            InitializeComponent();
        }

        private void newSceneToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dlgOpen.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Bitmap bmp = new Bitmap(Image.FromFile(dlgOpen.FileName));
                using (var f = new NewSceneForm(bmp))
                {
                    if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        if (scene != null)
                            scene.Dispose();
                        scene = new Scene(f.RenderModel, f.Transform);
                        SceneChanged();
                    }
                }

            }
        }

        private void SceneChanged()
        {
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix(scene.Transform.projection.M);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadMatrix(scene.Transform.modelView.M);
            perspectiveView.Refresh();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            GL.Enable(EnableCap.DepthTest);
        }

        private void perspectiveView_Resize(object sender, EventArgs e)
        {
            if (perspectiveView.ClientSize.Height == 0)
                perspectiveView.ClientSize = new System.Drawing.Size(perspectiveView.ClientSize.Width, 1);
            GL.Viewport(perspectiveView.ClientRectangle);
        }

        private void perspectiveView_Paint(object sender, PaintEventArgs e)
        {
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            if (scene != null)
                scene.Background.Draw();
            perspectiveView.SwapBuffers();
        }
    }
}
